package com.ruoyi.his.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.his.domain.*;
import com.ruoyi.his.mapper.HisOutpatientQueueMapper;
import com.ruoyi.his.mapper.HisPatientMapper;
import com.ruoyi.his.mapper.HisRegisterMapper;
import com.ruoyi.his.service.IHisPatientService;
import com.ruoyi.his.service.IHisRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.his.mapper.HisDepartmentMapper;
import com.ruoyi.his.service.IHisDepartmentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 挂号收费Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-04
 */
@Service
public class HisDepartmentServiceImpl implements IHisDepartmentService
{
    @Autowired
    private HisDepartmentMapper hisDepartmentMapper;

    @Autowired
    private HisPatientMapper hisPatientMapper;


    @Autowired
    private HisRegisterMapper hisRegisterMapper;

    @Autowired
    private HisOutpatientQueueMapper hisOutpatientQueueMapper;



    /**
     * 查询挂号收费
     *
     * @param id 挂号收费主键
     * @return 挂号收费
     */
    @Override
    public HisDepartment selectHisDepartmentById(String id)
    {
        return hisDepartmentMapper.selectHisDepartmentById(id);
    }

    @Override
    public List<Map<String, Object>> selectDoctorByDepartment(String id) {
        return hisDepartmentMapper.selectDoctorByDepartment(id);
    }

    /**
     * 查询挂号收费列表
     *
     * @param hisDepartment 挂号收费
     * @return 挂号收费
     */
    @Override
    public List<HisDepartment> selectHisDepartmentList(HisDepartment hisDepartment)
    {
        return hisDepartmentMapper.selectHisDepartmentList(hisDepartment);
    }

    /**
     * 新增挂号收费
     *
     * @param hisDepartment 挂号收费
     * @return 结果
     */
    @Override
    public int insertHisDepartment(HisDepartment hisDepartment)
    {
        return hisDepartmentMapper.insertHisDepartment(hisDepartment);
    }

    /**
     * 修改挂号收费
     *
     * @param hisDepartment 挂号收费
     * @return 结果
     */
    @Override
    public int updateHisDepartment(HisDepartment hisDepartment)
    {
        return hisDepartmentMapper.updateHisDepartment(hisDepartment);
    }

    /**
     * 批量删除挂号收费
     *
     * @param ids 需要删除的挂号收费主键
     * @return 结果
     */
    @Override
    public int deleteHisDepartmentByIds(String[] ids)
    {
        return hisDepartmentMapper.deleteHisDepartmentByIds(ids);
    }

    /**
     * 删除挂号收费信息
     *
     * @param id 挂号收费主键
     * @return 结果
     */
    @Override
    public int deleteHisDepartmentById(String id)
    {
        return hisDepartmentMapper.deleteHisDepartmentById(id);
    }

    /**
     * 1.通过病人就诊卡查询病人详细信息
     * 2.将前端传递的医生详细信息和病人信息添加到病人队列表中
     *
     * @param
     * @return
     */
    @Override
    @Transactional
    public String registered(Map data) {

        String msg  = "";

        String cardId = (String) data.get("cardId");

        System.out.println(data.get("doctorInfor"));
        // 根据病人就诊卡查询病人信息
        HisPatient patientInfor = hisPatientMapper.queryHisPatientByCardId(cardId);

        if(null==patientInfor){
            msg="没有该病人档案!!!";
            return msg;
        }

        List<HisRegister> hisRegisters = hisRegisterMapper.queryHisRegisterByPatientId(patientInfor.getId());

        // 获取医生详细信息
        Map map = (Map) data.get("doctorInfor");        //拿到医生对象
        //实例化医生对象,并给对象赋值 拿到医生对象
        HisUser doctorInfor = new HisUser();
        doctorInfor.setTreatmentPrice(map.get("treatmentPrice").toString());
        doctorInfor.setEmailStatus(Long.parseLong(map.get("emailStatus").toString()));
        doctorInfor.setGrade(map.get("grade").toString());
        doctorInfor.setNowNum(Long.parseLong(map.get("nowNum").toString()));
//        doctorInfor.setUpdateTime(map.get("updateTime").toString());
        doctorInfor.setId(map.get("id").toString());
        doctorInfor.setUserName(map.get("userName").toString());
        doctorInfor.setAllowNum(Long.parseLong(map.get("allowNum").toString()));
        doctorInfor.setDepartment(Long.parseLong(map.get("department").toString()));
        doctorInfor.setUserId(Long.parseLong(map.get("userID").toString()));
        doctorInfor.setWorkAddress(map.get("workAddress").toString());



        // 挂号收费表
        HisRegister hisRegister = new HisRegister();

        System.out.println(hisRegister+"------------------------------------------------");
        // 挂号收费表主键 关联患者队列表的付款外键
        String id = "SF_"+UUID.randomUUID().toString().replace("-", "");
        String createtime = DateUtils.getDate();
        hisRegister.setId(id);
        hisRegister.setCreateDatetime(createtime);
        hisRegister.setDepartment(doctorInfor.getDepartment()+"");
        hisRegister.setDoctor(doctorInfor.getUserName());
        hisRegister.setPayType(data.get("payType").toString());

        //非10 即普通门诊
        if(10==doctorInfor.getDepartment()){
            hisRegister.setRegisterType("急诊");
        }else{
            hisRegister.setRegisterType("普通门诊");
        }
        // 获取挂号费用
        hisRegister.setTreatmentPrice(data.get("treatmentPrice").toString());
        hisRegister.setPatientId(patientInfor.getId());
        //获取到当前用户
        SysUser user = SecurityUtils.getLoginUser().getUser();
        hisRegister.setOperatorName(user.getUserName());
        //已挂号
        hisRegister.setRegisterStatus(1l);
        //未就诊
        hisRegister.setTreatmentStatus(0l);
        hisRegister.setDoctorId(doctorInfor.getId());
        hisRegister.setChargeStatus(0l);


        //患者队列表
        HisOutpatientQueue hisOutpatientQueue=new HisOutpatientQueue();
        //UUID
        String hid = UUID.randomUUID().toString().replace("-", "");
        hisOutpatientQueue.setId(hid);
        //创建时间
        String date = DateUtils.getDate();
        hisOutpatientQueue.setCreateDatetime(date);
        //排队状态
        hisOutpatientQueue.setOutpatientQueueStatus(1l);
        //描述关系
        hisOutpatientQueue.setDescription(patientInfor.getName()+"#"+doctorInfor.getUserName());
        //付款外键
        hisOutpatientQueue.setRegisterId(hisRegister.getId());
        //医生外键
        hisOutpatientQueue.setUserId(doctorInfor.getId());
        //病人外键
        hisOutpatientQueue.setPatientId(patientInfor.getId());


        // 病人第一次进行挂号处理
        if(hisRegisters.size()==0){

            try {
                //将病人信息和医生信息添加到患者对列表  table: "his_outpatient_queue"
                hisOutpatientQueueMapper.insertHisOutpatientQueue(hisOutpatientQueue);
                //将病人信息初始化到挂号收费表中     table: "his_register"
                hisRegisterMapper.insertHisRegister(hisRegister);
                hisRegisterMapper.updataYsNowNum(doctorInfor.getId(),1);
                msg = "挂号成功";
                return msg;
            } catch (Exception e) {
                e.printStackTrace();
                msg = "挂号失败";
                return msg;
            }
        }else {


            for (HisRegister register : hisRegisters) {
                Long registerStatus = register.getRegisterStatus();
                Long treatmentStatus = register.getTreatmentStatus();
                Long chargeStatus = register.getChargeStatus();
                //已挂号
                if (registerStatus == 1 && treatmentStatus == 0) {
                    //未就诊情况下
//                    if (treatmentStatus == 0) {
                        String createDate = register.getCreateDatetime();
                        String nowDate = DateUtils.getDate();
                        //当天情况下
                        if (nowDate.equals(createDate)) {
                            //检查门诊队列是否待处理状态
                            HisOutpatientQueue outpatientQueue = hisOutpatientQueueMapper.queryHisOutpatientQueueByRegisterId(register.getId());
                            if (outpatientQueue != null && outpatientQueue.getOutpatientQueueStatus() == 1) {
                                String doctorName = Arrays.asList(outpatientQueue.getDescription().split("#")).get(1);
                                msg = "当日有未完成的就诊，请完成就诊！门诊医生：" + doctorName;
                                return msg;
                            }
                        } else {
                            try {
                                //将病人信息和医生信息添加到患者对列表  table: "his_outpatient_queue"
                                hisOutpatientQueueMapper.insertHisOutpatientQueue(hisOutpatientQueue);
                                //将病人信息初始化到挂号收费表中     table: "his_register"
                                hisRegisterMapper.insertHisRegister(hisRegister);
                                //修改医生现存就诊病人数量  +1
                                hisRegisterMapper.updataYsNowNum(doctorInfor.getId(),1);

                                msg = "挂号成功";
                                return msg;

                            } catch (Exception e) {
                                e.printStackTrace();
                                msg = "挂号失败";
                                return msg;

                            }
                        }
//                    }
                    //已就诊未缴费
                    if (treatmentStatus == 1 && chargeStatus == 0) {
                        msg = "存在已就诊未收费的记录，请及时缴费！";
                        return msg;

                    }

                } else { //未挂号 去挂号
                    try {
                        //将病人信息和医生信息添加到患者对列表  table: "his_outpatient_queue"
                        hisOutpatientQueueMapper.insertHisOutpatientQueue(hisOutpatientQueue);
                        //将病人信息初始化到挂号收费表中     table: "his_register"
                        hisRegisterMapper.insertHisRegister(hisRegister);
                        //修改医生现存就诊病人数量  +1
                        hisRegisterMapper.updataYsNowNum(doctorInfor.getId(),1);
                        msg = "挂号成功";
                        return msg;
                    } catch (Exception e) {
                        e.printStackTrace();
                        msg = "挂号失败";
                        return msg;
                    }
                }
            }
        }

        return msg;

    }
}
